import { Models } from '@/common/typings/modle'
import { HttpException, ParameterException, Success } from '@/core/HttpException'
import { command } from '@/server/mysql'

const commonDel = (options: any) => async (ctx: Models.Ctx) => {
  const { id, ids } = ctx.request.body as any
  if (!(ids || id || id === 0)) {
    throw new ParameterException()
  }
  let where = `WHERE ${ids?.length ? `id IN (${ids.map((item: any) => `'${item}'`).join()})` : `id = '${id}'`}`
  if (options.beforeFn) {
    await options.beforeFn(where)
  }
  const res = (await command(`DELETE FROM ${options.tableName} ${where};`, true)).results
  if (res.affectedRows === 1) {
    throw new Success({ msg: '删除成功' })
  }
  throw new HttpException({ msg: '删除时遇到问题：' + JSON.stringify(res) })
}
export default commonDel
